package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.HandleaffairsServiceSecondaryLegalActivity;
import com.ln.lnzw.activity.HandleaffairsServiceSecondaryNaturalActivity;
import com.ln.lnzw.adapter.ServiceObjectLegalAdapter;
import com.ln.lnzw.adapter.ServiceObjectNaturalAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.bean.ServiceObjectBean;
import com.ln.lnzw.net.HttpMethods;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/16.
 * 服务对象
 */

public class ServiceObjectFragment extends BaseFragment {
    @BindView(R.id.rv_ziranren_recyclerview)
    RecyclerView rvZiranrenRecyclerview;
    @BindView(R.id.rv_raren_recyclerview)
    RecyclerView rvRarenRecyclerview;
    Unbinder unbinder;
    private ServiceObjectNaturalAdapter serviceObjectNaturalAdapter;
    private ServiceObjectLegalAdapter serviceObjectLegalAdapter;
    private List<ServiceObjectBean.ResultBean> zList = new ArrayList<>();//自然人集合
    private List<ServiceObjectBean.ResultBean> fList = new ArrayList<>();//法人集合
    private GridLayoutManager zGridLayoutManager;
    private GridLayoutManager fGridLayoutManager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.service_object_fragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initDataNaturalPerson();//自然人
    }
    //法人
    private void initDataLegalPerson() {
        HttpMethods.getInstanceCenter().info.getNewNaturalAndLegal(AppConstant.LEGAL_PERSON_URL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ServiceObjectBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ServiceObjectBean serviceObjectBean) {
                        if (fList != null || !fList.isEmpty()) {
                            fList.clear();
                        }
                        fList.addAll(serviceObjectBean.getResult());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        serviceObjectNaturalAdapter.notifyDataSetChanged();
                        serviceObjectLegalAdapter.notifyDataSetChanged();
                    }
                });
    }


   //自然人
    private void initDataNaturalPerson() {
        HttpMethods.getInstanceCenter().info.getNewNaturalAndLegal(AppConstant.NATURAL_PERSON_URL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ServiceObjectBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ServiceObjectBean serviceObjectBean) {
                        if (zList != null || !zList.isEmpty()) {
                            zList.clear();
                        }
                        if ("200".equals(serviceObjectBean.getCode())){
                            zList.addAll(serviceObjectBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        initDataLegalPerson();//法人
                    }
                });
    }



    private void initView() {
        zGridLayoutManager = new GridLayoutManager(activity, 4);
        rvZiranrenRecyclerview.setLayoutManager(zGridLayoutManager);
        rvZiranrenRecyclerview.setNestedScrollingEnabled(false);
        serviceObjectNaturalAdapter = new ServiceObjectNaturalAdapter(R.layout.item_handleaffairs, zList);
        rvZiranrenRecyclerview.setAdapter(serviceObjectNaturalAdapter);
        rvZiranrenRecyclerview.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i("789", "onSimpleItemClick: "+"自然人"+position);
                Bundle b = new Bundle();
                b.putString("cDesc", zList.get(position).getCode());
                ActivityUtils.startActivity(b, activity, HandleaffairsServiceSecondaryNaturalActivity.class);
            }
        });
        fGridLayoutManager = new GridLayoutManager(activity, 4);
        rvRarenRecyclerview.setLayoutManager(fGridLayoutManager);
        rvRarenRecyclerview.setNestedScrollingEnabled(false);
        serviceObjectLegalAdapter = new ServiceObjectLegalAdapter(R.layout.item_handleaffairs, fList);
        rvRarenRecyclerview.setAdapter(serviceObjectLegalAdapter);
        rvRarenRecyclerview.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i("789", "onSimpleItemClick: "+"法人"+position);
                Log.i("789", "onSimpleItemClick: "+"法人"+fList.size());

                Bundle b = new Bundle();
                b.putString("cDesc", fList.get(position).getCode());
                ActivityUtils.startActivity(b, activity, HandleaffairsServiceSecondaryLegalActivity.class);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
